package com.xilihui.xlh.business.activitys.store;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.ApplyActivity;
import com.xilihui.xlh.business.dialogs.AddressPickerDialog;
import com.xilihui.xlh.business.entities.AddressEntity;
import com.xilihui.xlh.business.entities.AreaEntity;
import com.xilihui.xlh.business.requests.SignedRequest;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.TextExtend;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;
import org.feezu.liuli.timeselector.view.PickerView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/6.
 */

public class AddAddressActivity extends ToolBaseCompatActivity{
    
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_phoneNum)
    EditText et_phone;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.et_details)
    EditText et_details;
    @BindView(R.id.cb_defalut)
    CheckBox checkBox;
    
    
    
    String name = "";
    String phone = "";
    String province_id = "";
    String city_id = "";
    String district_id = "";
    String address = "";
    String is_default = "1";
    
    AddressEntity.AddressListBean addressListBean = null;
    boolean xiugai = false;
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void save(){
        if(xiugai){
            updateData();            
        }else {
            submit();
        }
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_add_address;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("添加收货地址");
        setToolRightText("保存");
        
        
        addressListBean = (AddressEntity.AddressListBean) getIntent().getSerializableExtra("data");
        
        if(addressListBean!=null){
            name = addressListBean.getConsignee();
            phone = addressListBean.getMobile();
            province_id = addressListBean.getProvince().getId();
            city_id = addressListBean.getCity().getId();
            district_id = addressListBean.getDistrict().getId();
            
            et_name.setText(name);
            et_name.setSelection(name.length());
            et_phone.setText(phone);
            et_phone.setSelection(phone.length());
            
            tv_address.setText(addressListBean.getProvince().getName()+" "+addressListBean.getCity().getName()
            +" "+addressListBean.getDistrict().getName());
            
            String details = addressListBean.getAddress();
            et_details.setText(details);
            et_details.setSelection(details.length());
            
            xiugai = true;
        }
        
    }

    @Override
    public void initData() {

    }
    
    @OnClick(R.id.ll_address)
    public void selectAddress(){

        
       

        AreaEntity areaEntity = (AreaEntity)SPUtil.getObject(SPUtil.ADDRESS);
        if(areaEntity!=null){
            AddressPickerDialog addressPickerDialog = new AddressPickerDialog(this);
            addressPickerDialog.setCallback(new AddressPickerDialog.Callback() {
                @Override
                public void onSelected(String province, String city, String county, String provinceId, String cityId, String countyId) {
                    tv_address.setText(province+" "+city+" "+county);
                    province_id = provinceId;
                    city_id = cityId;
                    district_id = countyId;
                }
            });

            addressPickerDialog.setData(areaEntity);
            addressPickerDialog.show();
        }else {
            address();
        }
    }
    
    public void submit(){
        
        name = et_name.getText().toString();
        phone = et_phone.getText().toString();
        String diqu = tv_address.getText().toString();
        address = et_details.getText().toString();
        
        
        if(TextUtil.isEmpty(name)){
            ToastUtil.toastShortNegative("请输入收货人");
            return;
        }
        
        if(TextUtil.isEmpty(phone)&& !TextExtend.isMobile(phone)){
            ToastUtil.toastShortNegative("请输入正确的手机号码");
            return;
        }
        
        if(TextUtil.isEmpty(diqu)){
            ToastUtil.toastShortNegative("请选择所在地区");
            return;
        }
        
        if(TextUtil.isEmpty(address)){
            ToastUtil.toastShortNegative("请输入详情地址");
            return;
        }
        
        if(checkBox.isChecked()){
            is_default = "1";
        }else {
            is_default = "0";
        }
        
        StoreRequest.addAddress(this,name,province_id,city_id,district_id,phone,is_default,address)
                    .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                    .compose(this.<BaseEntity>bindToLifecycle())
                    .subscribe(new HttpSubscriber<BaseEntity>(this) {
                        @Override
                        protected void onSuccess(BaseEntity entity) {
                            ToastUtil.toastShortPositive(entity.getMsg());
                            finish();
                            YEventBuses.post(new YEventBuses.Event("add_address"));
                        }

                        @Override
                        protected void onFail(String msg) {
                            ToastUtil.toastShortNegative(msg);
                        }
                    });
        
    }

    public void address(){

        SignedRequest.getAddress(this)
                .compose(DoTransform.<AreaEntity>applyScheduler(this,true))
                .compose(this.<AreaEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<AreaEntity>(this) {
                    @Override
                    protected void onSuccess(AreaEntity entity) {
                        AddressPickerDialog addressPickerDialog = new AddressPickerDialog(AddAddressActivity.this);
                        addressPickerDialog.setCallback(new AddressPickerDialog.Callback() {
                            @Override
                            public void onSelected(String province, String city, String county, String provinceId, String cityId, String countyId) {
                                tv_address.setText(province+" "+city+" "+county);
                                province_id = provinceId;
                                city_id = cityId;
                                district_id = countyId;
                            }
                        });

                        addressPickerDialog.setData(entity);
                        addressPickerDialog.show();
                        SPUtil.putObject(SPUtil.ADDRESS,entity);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });

    }
    
    public void updateData(){

        name = et_name.getText().toString();
        phone = et_phone.getText().toString();
        String diqu = tv_address.getText().toString();
        address = et_details.getText().toString();


        if(TextUtil.isEmpty(name)){
            ToastUtil.toastShortNegative("请输入收货人");
            return;
        }

        if(TextUtil.isEmpty(phone)&& !TextExtend.isMobile(phone)){
            ToastUtil.toastShortNegative("请输入正确的手机号码");
            return;
        }

        if(TextUtil.isEmpty(diqu)){
            ToastUtil.toastShortNegative("请选择所在地区");
            return;
        }

        if(TextUtil.isEmpty(address)){
            ToastUtil.toastShortNegative("请输入详情地址");
            return;
        }

        if(checkBox.isChecked()){
            is_default = "1";
        }else {
            is_default = "0";
        }
        
        
        StoreRequest.updateAddress(AddAddressActivity.this,addressListBean.getAddress_id(),name,province_id,
                city_id,district_id,phone,is_default,address)
                .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                .compose(this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        finish();
                        YEventBuses.post(new YEventBuses.Event("add_address"));
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
}
